<?php

namespace App\Http\Controllers\LiveAdmin;

use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Models\LiveAdmin;

use App\User;
use http\Client\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Modules\Edu\Http\Controllers\Front\ChatController;

class LiveAdminController extends Controller
{
    //库中被踢出的user和GateWay:Group在线的人，

    public function index(Request $request)
    {

        $live_id = $request->input('live');
        $field = $request->input('model') ? 'kickout_endtime' : 'speek_endtime';

        //获取直播间在线的全部人数，并排除已经禁言或踢出的
        $online = app(ChatController::class)->getGroupUserInfo($live_id);

        //只提取，被禁言或被踢的名单
        $dbData = $this->getDbData($live_id, $field);
        //将在线人员和库中人员进行合并，进行id去重。 键和值对调，利用键的唯一性进行键合并保留唯一键再重排键为一个数组。
        $onlineKey = array_flip(Arr::pluck($online, 'id'));
        $dataKey = array_flip($dbData->map(function ($item, $key) {
            return $item->user_id;
        })->toArray());

        $dataAllKey = array_keys($onlineKey + $dataKey);
        //提取库中被踢的人和在线的交集

        $dataAll = User::find($dataAllKey)->toArray();
        $dbData = User::find($dbData)->toArray();

        //筛选只保留id和name

        $rightData = filterJsonArrayKey($dbData, ['id']);

        $rightData = array_map(function ($item) {
            return $item['id'];
        }, $rightData);

        $dataAll = filterJsonArrayKey($dataAll, ['id', 'name']);

        return response()->json(['data' => $dataAll, 'rightData' => $rightData, 'message' => '数据已刷新']);
    }

    public function getDbData($live_id, $field)
    {
        $time = date('Y-m-d H:i:s');
        $dbData = LiveAdmin::select('user_id')->where('module_id', module()['id'])->where('site_id', site()['id'])->where('live_id', $live_id)->where($field, '>', $time)->get();
        return $dbData;
    }

    public function speek(Request $request)
    {
        $uids = $request->input('uids');
        $time = $request->input('time');
        $removeId = $request->input('removeId');

        $this->setSpeek($uids, $time);
        $this->removeSpeek($removeId);
    }

    public function kickout(Request $request)
    {
        $uids = $request->input('uids');
        $time = $request->input('time');
        $removeId = $request->input('removeId');
        $speekMessage = $request->input('speekMessage');
        $removeMessage = $request->input('removeMessage');

        $this->setKickout($uids, $time);
        $this->removeKickout($removeId);
    }

    //禁言和踢出的原理 记录禁言时间入库，在入库后给消息，在观众进入直播间前，进行验证，失败转到直播间首页
    protected function setSpeek($uids, $time, $message = '已经被禁言')
    {
        $live_id = \request()->input('live_id');
        if (is_null($live_id)) {
            return;
        }
        $time = date('Y-m-d H:i:s', time() + $time * 60);
        $uids = is_array($uids) ? $uids : [$uids];

        foreach ($uids as $uid) {
            $liveAdmin = LiveAdmin::updateOrCreate(['user_id' => $uid, 'site_id' => 1, 'live_id' => $live_id, 'module_id' => module()['id']], ['speek_endtime' => $time]);
            app(ChatController::class)->systemSend($uid, $message, 'speek');
            app(ChatController::class)->sendToGroupAll($live_id, User::find($uid)->name . $message, '系统消息');
        }

        return;
    }

    protected function setKickout($uids, $time, $message = '已经被踢出房间')
    {
        $live_id = \request()->input('live_id');
        $time = date('Y-m-d H:i:s', time() + $time * 60);
        if (is_null($live_id)) {
            return;
        }
        $uids = is_array($uids) ? $uids : [$uids];
        if (is_array($uids)) {
            foreach ($uids as $uid) {
                LiveAdmin::updateOrCreate(['user_id' => $uid, 'site_id' => site()['id'], 'live_id' => $live_id, 'module_id' => module()['id']], ['kickout_endtime' => $time]);
                $chatController = app(ChatController::class);
                //广播消息
                $chatController->sendToGroupAll($live_id, User::find($uid)->name . $message, '系统消息');

                //断开直播间链接
                $chatController->closeClient($uid);

            }
        }


    }

    //解除禁言
    protected function removeSpeek($uid, $message = '解除禁言')
    {
        $live_id = \request()->input('live_id');
        $time = date('Y-m-d H:i:s', 0);
        $uids = is_array($uid) ? $uid : [$uid];
        //获取禁言前的列表
        $dbData = $this->getDbData($live_id, 'speek_endtime');
        //解除禁言
        LiveAdmin::whereIn('user_id', $uids)->where('live_id', $live_id)->where('module_id', module()['id'])->update(['speek_endtime' => $time]);

        $uids = $dbData->pluck('user_id')->intersect($uid);
        foreach ($uids as $uid) {
            app(ChatController::class)->systemSend($uid, $message, 'speek');
        }
    }

    //解除踢人
    protected function removeKickout($uid, $message = '已经可以进入房间')
    {

        $live_id = \request()->input('live_id');
        $time = date('Y-m-d H:i:s', 0);
        $uid = is_array($uid) ? $uid : [$uid];

        LiveAdmin::whereIn('user_id', $uid)->where('live_id', $live_id)->where('module_id', module()['id'])->update(['kickout_endtime' => $time]);


    }

//查询用户房管
    public function search(Request $request)
    {

        if ($name = $request->name) {
            $users = User::Search($name)->limit(10)->get();
        } else {
            $users = User::limit(10)->get();
        }
        //resource相当于对模型再一次进行处理，然后如果处理集合需要collection类方法。
        //collection是集合如果处理元数据，只能是collection类
        return UserResource::collection($users);
    }
//设置房管
    public function setLiveAdmin(){
        $user=user();

        $user->assignRole('liveAdmin');
    }
}
